//
//  OriginalBolgView.swift
//  SinaBlog
//
//  Created by 有何不可 on 16/1/6.
//  Copyright © 2016年 有何不可. All rights reserved.
//

import UIKit
import SnapKit

class OriginalBolgView: UIView {
    
    var origBlogConstraint: Constraint?
    var originalBolgModel: PersonalBlogViewModel? {
        
        didSet {
            
            titleLal.text = originalBolgModel?.statuse?.user?.screen_name
            headImgView.sd_setImageWithURL(originalBolgModel?.headImgURL)
            VIPView.image = originalBolgModel?.VIPImg
            verifitiedImgView.image = originalBolgModel?.verifitiedImage
            timeLal.text = originalBolgModel?.creat_at
            sorceLal.text = originalBolgModel?.scource
            contentLal.text = originalBolgModel?.statuse?.text
            
            origBlogConstraint?.uninstall()
            
            if originalBolgModel?.statuse?.pic_urls?.count > 0 {
                
                pictureView.hidden = false
                
                pictureView.picUrls = originalBolgModel?.statuse?.pic_urls
                
                snp_updateConstraints(closure: { (make) -> Void in
                    origBlogConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(OriginalBlogMargin).constraint
                })
                
            } else {
                
                snp_updateConstraints(closure: { (make) -> Void in
                    origBlogConstraint = make.bottom.equalTo(contentLal.snp_bottom).offset(OriginalBlogMargin).constraint
                })
                
                pictureView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        backgroundColor = UIColor(white: 240 / 255, alpha: 1.0)
        setUI()
        backgroundColor = UIColor.whiteColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUI() {
        
        addSubview(headImgView)
        addSubview(titleLal)
        addSubview(verifitiedImgView)
        addSubview(VIPView)
        addSubview(timeLal)
        addSubview(sorceLal)
        addSubview(contentLal)
        addSubview(pictureView)
        
        headImgView.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(50)
            make.height.equalTo(50)
            make.top.equalTo(self.snp_top).offset(OriginalBlogMargin)
            make.left.equalTo(self.snp_left).offset(OriginalBlogMargin)
        }
        
        titleLal.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImgView.snp_right).offset(OriginalBlogMargin)
            make.top.equalTo(headImgView.snp_top).offset(OriginalBlogMargin)
        }
        
        verifitiedImgView.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(titleLal.snp_centerY)
            make.left.equalTo(titleLal.snp_right).offset(OriginalBlogMargin)
        }
        
        VIPView.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(titleLal.snp_centerY)
            make.left.equalTo(verifitiedImgView.snp_right).offset(OriginalBlogMargin)
            
        }
        
        timeLal.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(titleLal.snp_bottom).offset(OriginalBlogMargin)
            make.left.equalTo(headImgView.snp_right).offset(4)
            
        }
        
        sorceLal.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(timeLal.snp_centerY)
            make.left.equalTo(timeLal.snp_right).offset(OriginalBlogMargin)
            
        }
        
        contentLal.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(headImgView.snp_bottom).offset(OriginalBlogMargin)
            make.left.equalTo(headImgView.snp_left)
            make.width.equalTo(UIScreen.mainScreen().bounds.width - 20)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLal.snp_bottom).offset(OriginalBlogMargin)
            make.left.equalTo(contentLal.snp_left)
            
            //            make.width.equalTo(100)
            //            make.height.equalTo(100)
        }
        
        snp_makeConstraints { (make) -> Void in
            origBlogConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
        
    }
    
    private lazy var headImgView: UIImageView = {
        
        let imgView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        imgView.layer.cornerRadius = 25
        
        imgView.clipsToBounds = true
        
        return imgView
        }()
    
    private lazy var titleLal: UILabel = {
        
        let lal = UILabel()
        
        lal.text = "昵称"
        
        lal.font = UIFont.systemFontOfSize(14)
        
        lal.textColor = UIColor.blackColor()
        
        return lal
        }()
    
    private lazy var verifitiedImgView: UIImageView = {
        
        let imgView = UIImageView(image: UIImage(named: "avatar_enterprise_vip"))
        
        return imgView
        }()
    
    private lazy var VIPView: UIImageView = {
        
        let imgView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
        
        return imgView
        }()
    
    private lazy var timeLal: UILabel = {
        
        let lal = UILabel()
        
        lal.font = UIFont.systemFontOfSize(12)
        
        lal.textColor = UIColor.orangeColor()
        
        lal.text = "时间"
        
        return lal
        
        }()
    
    private lazy var sorceLal: UILabel = {
        
        let lal = UILabel()
        
        lal.text = "来源"
        
        lal.font = UIFont.systemFontOfSize(12)
        
        lal.textColor = UIColor.darkGrayColor()
        
        return lal
        }()
    
    private lazy var contentLal: UILabel = {
        
        let lal = UILabel()
        
        lal.font = UIFont.systemFontOfSize(14)
        
        lal.textColor = UIColor.blackColor()
        
        lal.text = "牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋"
        
        lal.numberOfLines = 0
        
        let space = NSMutableAttributedString(attributedString: lal.attributedText!)
        
        let paragraphStyle = NSMutableParagraphStyle()
        
        paragraphStyle.lineHeightMultiple = 100000
        
        space.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, lal.text?.characters.count ?? 0))
        
        lal.attributedText = space
        
        lal.sizeToFit()

        
        return lal
        }()
    
    
    private lazy var pictureView: PictureView = PictureView()
}
